package com.googlecode.perfectable.visuable.visual.custom;

import com.google.common.collect.ImmutableList;
import com.googlecode.perfectable.visuable.renderer.ElementRenderer;
import com.googlecode.perfectable.visuable.renderer.VisualRenderer;
import com.googlecode.perfectable.visuable.visual.Visual;

public class ScriptVisual implements Visual {
	private static final String ELEMENT_NAME = "script";

	private static final String FRAMEWORK_SCRIPT_TYPE = "application/javascript";
	private static final ImmutableList<String> FRAMEWORK_SCRIPT_LOCATIONS =
			ImmutableList.<String> builder()
					.add("scripts/base.js")
					.add("scripts/channel.js")
					.add("scripts/visual.js")
					.add("scripts/main.js")
					.build();

	private static final String LOCATION_ATTRIBUTE_NAME = "src";
	private static final String TYPE_ATTRIBUTE_NAME = "type";

	private final String location;
	private final String type;

	protected ScriptVisual(String location, String type) {
		this.location = location;
		this.type = type;
	}

	public static Iterable<ScriptVisual> createFramework() {
		ImmutableList.Builder<ScriptVisual> visualsBuilder = ImmutableList.builder();
		for(String location : FRAMEWORK_SCRIPT_LOCATIONS) {
			visualsBuilder.add(createExternal(location, FRAMEWORK_SCRIPT_TYPE));
		}
		return visualsBuilder.build();
	}

	public static ScriptVisual createExternal(String location, String type) {
		return new ScriptVisual(location, type);
	}

	@Override
	public void render(VisualRenderer painter) {
		try(ElementRenderer nestedPainter = painter.element(ELEMENT_NAME)) {
			nestedPainter.attribute(LOCATION_ATTRIBUTE_NAME, this.location);
			nestedPainter.attribute(TYPE_ATTRIBUTE_NAME, this.type);
		}
	}

}
